package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQueryObject;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentMongoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentMongoServiceImpl implements ITravelCommentMongoService{

    @Autowired
    private TravelCommentMongoRepository repository;
    //注入MongoTemplate
    @Autowired
    private MongoTemplate template;
    @Override
    public TravelComment save(TravelComment entity) {
        //填充评论时间
        entity.setCreateTime(new Date());
        //区别处理回复评论
        if(entity.getType()== TravelComment.TRAVEL_COMMENT_TYPE){
            //根据id查询到源评论
            TravelComment ref = repository.findById(entity.getRefComment().getId()).get();
            //设置给当前评论的引用
            entity.setRefComment(ref);
        }
        repository.save(entity);
            return entity;
    }

    @Override
    public List<TravelComment> listByTraveId(Long travelId) {
        return repository.findByTravelId(travelId);
    }
}
